﻿var shareLocationID;

function doEditLocation()
{
    id = document.getElementById('lbIdLocation').value;
    name = document.getElementById('txtNameLocation').value;
    address= document.getElementById('txtAddress').value;
    description =document.getElementById('txtDescription').value;
    idcategory = document.getElementById('listCatalog').value;
    latlng = document.getElementById('lbLatLng').value;

    params = "id="+id+"&name="+name +"&address="+address+"&desciption="+description+"&idcategory="+idcategory+"&latlng="+latlng;
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "edit");
    request.send(params);
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4)
        {   
            if (request.responseText=='True')
                alert("Sửa thành công");
            else
                alert("Sửa thất bại");
        }
    }
    showDefaulPanelForUser()
    reloadTreeLocation();
}

function doAddLocation()
{   
    name = document.getElementById('txtNameLocation').value;
    address= document.getElementById('txtAddress').value;
    description =document.getElementById('txtDescription').value;
    idcategory = document.getElementById('listCatalog').value;
    latlng = document.getElementById('lbLatLng').value;

    params = "name="+name +"&address="+address+"&desciption="+description+"&idcategory="+idcategory+"&latlng="+latlng;
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "add");
    request.send(params);

    document.getElementById('txtNameLocation').value="";
    document.getElementById('txtAddress').value="";
    document.getElementById('txtDescription').value="";
    document.getElementById('listCatalog').value="";
    document.getElementById('lbLatLng').value="";
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4)
        {   
            if (request.responseText=='True')
            {
                alert("Thêm thành công"); ;
            }
            else
                alert("Thêm thất bại");
        }
    }   
    showDefaulPanelForUser();
    reloadTreeLocation();
    
}

function doDeleteLocation(idLocation)
{
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "del");
    request.send("id="+idLocation);
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4)
        {   
            if (request.responseText=='True')
                alert("Đã xóa");
            else
                alert("Xóa thất bại");
        }
    }    
    showDefaulPanelForUser();
    reloadTreeLocation();
}

function shareLocation(idLocation)
{
    shareLocationID = idLocation;
    hideAllPanel();
    showControl('pShare');
}

function doShareLocation()
{
    var user = document.getElementById('ddlUserShare').value;
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "share");
    request.send("id="+shareLocationID + "&user="+user);
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4)
        {   
            if (request.responseText=='True')
                alert("Thành công");
            else
                alert("Thất bại");
        }
    }
    showDefaulPanelForUser();
}

function doCancel()
{
    showDefaulPanelForUser();
}

function doLogin()
{
    var request = new XMLHttpRequest();
    var username = $('#txtUserName').val();
    if(username==null || username=="" || username=="Tên đăng nhập"){
        alert("Bạn chưa nhập tên đăng nhập");
        return ;
    }
    
    var pass = $('#txtPassword').val();
    if(pass==null || pass==""){
        alert("Bạn chưa nhập password");
        return;
    }
    var params = "u="+username+"&p="+pass;                                                     
        
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "login");
    request.send(params);
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4)
        {             
            if (request.responseText!='false'){                
                var sign = document.getElementById('sign');
                sign.innerHTML = request.responseText +" | <a href='javascript:doLogout()'>Signout</a>";
                sign.style.display = 'inline';
                var unsign = document.getElementById('unsign');
                unsign.innerHTML='';
                reloadTreeLocationByUserName(username);
                //alert("do begin Check");
                checkForShare();                
            }
            else{                
                alert("Login invalid, try again !");
                document.getElementById("txtUserName").value="";
                document.getElementById("txtUserName").focus();
            }
            document.getElementById('lbUserName').value = request.responseText;
        }
    }
    
}

function doLogout(){   
    var request = new XMLHttpRequest();                                              
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "logout");
    request.send();
    
    var sign = document.getElementById('sign');
    sign.innerHTML = '';
    var unsign = document.getElementById('unsign');
    unsign.innerHTML = '<input class="inputLogin" id="txtUserName" type="text" onblur="return RemoveUserText()" onclick="return TempUserText()" value="Tên đăng nhập"/>&nbsp;';
    unsign.innerHTML+= '<input class="inputLogin" id="txtPassword" type="password" onblur="return RemovePassText()" onclick="return TempPassText()" value="password"/> ';
    unsign.innerHTML+= '<input id="cmdSubmit" type="button" class="button" value="Đăng nhập" onclick="javascript:doLogin()"/>';
    unsign.innerHTML+= '<a href="Register.aspx">Đăng ký</a>';
    unsign.innerHTML+= '<label id="lblMessage" width="100%"></label>';
    
    showDefaulPanelForGuest();
}

function reloadTreeLocation()
{
    reloadTreeLocationByUserName(document.getElementById('lbUserName').value);
}

function reloadTreeLocationByUserName(username)
{
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "reloadtree");
    request.send("username="+username);
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4  || request.readyState==3)
        {   
            if (request.responseText!='false'){                
                var tree = document.getElementById("treeLocation");
                tree.innerHTML = request.responseText;      
            }
        }
        //else
          //  alert(request.readyState + request.responseText);
    }
   
    showControl("pTreeView");
}

function moveIconClick(){
   var a=document.getElementById("content_left");
   var b=document.getElementById("moveicon");
   if(a.style.display=="none"){
        a.style.display="block";
        b.style.background= "url('/images/moveicon.png') no-repeat right top";
        b.style.left="18%";
   }
   else{
        a.style.display="none";
        b.style.background= "url('/images/moveicon-out.png') no-repeat right top";
        b.style.left="0";
   }
}

function TempUserText() {
    var txtuser = document.getElementById("txtUserName").value;
    if (txtuser == "Tên đăng nhập") {
        document.getElementById("txtUserName").value = "";
    }
}

function TempPassText() {    
    var txtpass = document.getElementById("txtPassword").value;
    if (txtpass == "password") {
        document.getElementById("txtPassword").value = "";
    }
}

function RemoveUserText() {
    var txtuser = document.getElementById("txtUserName").value;
    if (txtuser == "") {
        document.getElementById("txtUserName").value = "Tên đăng nhập";
    }
}

function RemovePassText() {    
    var txtpass = document.getElementById("txtPassword").value;
    if (txtpass == "") {
        document.getElementById("txtPassword").value = "password";
    }
}

function doSearchNearLocation()
{
    var keyword = document.getElementById('txtSearch2').value;
    var catelogy = document.getElementById('ddlCatelogy2').value;
    var numofresults = document.getElementById('ddlNumOfResults').value;
    var params = "keyword="+keyword + "&catelogy="+catelogy + "&latlng="+LatLng +"&numofresults="+numofresults;
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "search");
    request.send(params);
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4  || request.readyState==3)
        {   
            if (request.responseText!='false'){                
                var panel = document.getElementById("pSearch2Results");
                var panelContent = "<strong>Các kết quả tìm được:</strong></br>";
	            panelContent += request.responseText;
	            panel.innerHTML = panelContent;	            
            }
        }
    }
}

function checkForShare()
{
    //alert("begin check");
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "checkshare");
    request.send();
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4  || request.readyState==3)
        {   
            if (request.responseText!='false'){                
                var panel = document.getElementById("dShareContent");
	            panel.innerHTML = request.responseText;
	            showControl('pPopupShare');
            }
            else{
                hideControl('pPopupShare');
            }
        }
        //alert(request.readyState);
    }
}


function deny(idShare)
{
    // set state of idShare to -1.
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "deny");
    request.send('idshare='+idShare);
    
    request.onreadystatechange=function()
    {
        if(request.readyState==4  || request.readyState==3)
        {   
            // remove item suggest in share location popup window.
            hideControl(idShare);
        }
    }
}

function accept(idShare, idLocation)
{
    // set state of idShare to 1 and save location to location list of current user.
    var request = new XMLHttpRequest();
    request.open("POST", "Default.aspx", true);
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("action", "accept");
    var params = 'idshare='+idShare + '&idlocation='+idLocation;
    request.send(params);
    alert(params);
    request.onreadystatechange=function()
    {
        if(request.readyState==4  || request.readyState==3)
        {   
            // update location tree view of current user.
        }
         // remove item suggest in share location popup window.
         hideControl(idShare);
    }
}
